package com.lzhsite.technology.designPattern.Interpreter.FinanceDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class ClientForFinanceDemo
{
    public static void main(String[] args)
    {
        String expStr = getExpStr();
        HashMap<String, Integer> var = getValue(expStr);
        Calculator calculator = new Calculator(expStr);
        System.out.println("运算结果为：" + expStr + " = " + calculator.run(var));
    }

    public static String getExpStr()
    {
        String in = null;
        System.out.println("请输入表达式：");
        try
        {
            in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        } catch (IOException exception)
        {
            exception.printStackTrace();
        }
        return in;
    }

    public static HashMap<String, Integer> getValue(String expStr)
    {
        String in = null;
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        for (char ch : expStr.toCharArray())
        {
            if (ch != '+' && ch != '-')
            {
                if (!map.containsKey(String.valueOf(ch)))
                {
                    System.out.print("请输入" + ch + "的值：");
                    try
                    {
                        in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                    } catch (IOException exception)
                    {
                        exception.printStackTrace();
                    }
                    map.put(String.valueOf(ch), Integer.valueOf(in));
                }
            }
        }
        return map;
    }
}
